package com.hk.rabbitmq.six;

import com.hk.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: dpb
 * @date: 2022/3/22
 */
public class EmitLogDirect {
    private static final String EXCHANGE_NAME = "direct logs";
    public static void main(String[] args) throws Exception{

        //1、获取信道
        Channel channel = RabbitMqUtils.getChannel();
        //2、定义交换机类型
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

        //3、发送消息
        HashMap<String, String> bindingKeyMap = new HashMap<>();
        bindingKeyMap.put("info","普通 info 消息");
        bindingKeyMap.put("warning","警告 warning 消息");
        bindingKeyMap.put("error","错误 error 消息");

        bindingKeyMap.put("debug","调试 debug 消息");
        for (Map.Entry<String, String> bindingKeyEntry : bindingKeyMap.entrySet()) {
            String bindingKey = bindingKeyEntry.getKey();
            String message = bindingKeyEntry.getValue();

            channel.basicPublish(EXCHANGE_NAME,bindingKey,null,message.getBytes(StandardCharsets.UTF_8));
            System.out.println("生产者发出消息： " + message);
        }
    }
}
